﻿using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Text;

namespace AnnieSharp
{
    public static class ColorExtension
    {
        /// <summary>
        /// 将表示颜色的16进制字符串转化为SKcolor,支持#,0x一级6位
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        public static SKColor ToRGBSkColor(this string color)
        {
            color = color.ToUpper();

            if(color.StartsWith("0X"))
            {
                color = color.Substring(2);
            }

            if (color.StartsWith("#"))
            {
                color = color.Substring(1);
            }

            if (color.Length!=6)
            {
                throw new Exception("非法RGB颜色字符串");
            }

            string r = color.Substring(0, 2);
            string g = color.Substring(2, 2);
            string b = color.Substring(4, 2);

            byte red = Byte.Parse(r, System.Globalization.NumberStyles.HexNumber);
            byte green = Byte.Parse(g, System.Globalization.NumberStyles.HexNumber);
            byte blue = Byte.Parse(b, System.Globalization.NumberStyles.HexNumber);

            return new SKColor(red, green, blue);
        }

        /// <summary>
        /// 将alpha转换为SkColor中的16进制
        /// </summary>
        /// <param name="alpha"></param>
        /// <returns></returns>
        public static byte ToAlphaByte(this float alpha)
        {
            return (byte)(0xFF * (alpha));
        }
    }


}
